// Developed by Oguz Demirel a.k.a Zieglers
// Copyright (c) 2010 Oguz Demirel
// ****************************************
// Ajax RPC methods for SharePoint
// ****************************************

// ****************************************
// ************ MOVE DOCUMENTS ************ 
// ****************************************
function moveDocumentUsingRPC(from, to) {  

// Generate data string using src and dest urls
var strData = "method=move document:12.0.4518.1016&service_name=/&oldUrl=" + from + "&newUrl=" + to + "&url_list=[]&rename_option=findbacklinks&put_option=overwrite,createdir,migrationsemantics&docopy=false";

// Move document - RPC call using ajax
$.ajax({
  type: "POST",
  url: L_Menu_BaseUrl + "/_vti_bin/_vti_aut/author.dll",
  // Add http request headers
  beforeSend : function(xhr) {
         xhr.setRequestHeader('Content-Type','application/x-vermeer-urlencoded');
         xhr.setRequestHeader('X-Vermeer-Content-Type','application/x-vermeer-urlencoded');
  },
  data: strData,

  success: function(data, text){
  }, // end - success
  error: function (request, status, error) { 
        alert(request.responseText);
  } 

          
 }); // end - ajax

// refresh page
// window.location.href=window.location.href;

}// end - function

// ****************************************
// ********* END - MOVE DOCUMENTS *********
// ****************************************

// ****************************************
// ************ DELETE DOCUMENTS ************ 
// ****************************************
function deleteDocumentUsingRPC(from, to) {  

// Generate data string using src and dest urls
var strData = "method=remove documents:12.0.4518.1016&service_name=/&url_list=[" + from + "]";

// Delete document - RPC call using ajax
$.ajax({
  type: "POST",
  url: L_Menu_BaseUrl + "/_vti_bin/_vti_aut/author.dll",
  // Add http request headers
  beforeSend : function(xhr) {
         xhr.setRequestHeader('Content-Type','application/x-vermeer-urlencoded');
         xhr.setRequestHeader('X-Vermeer-Content-Type','application/x-vermeer-urlencoded');
  },
  data: strData,

  success: function(data, text){
         
  }, // end - success
  error: function (request, status, error) { 
        alert(request.responseText);
  } 

          
 }); // end - ajax

// refresh page
// window.location.href=window.location.href;

}// end - function
// ****************************************
// ********* END - DELETE DOCUMENTS *********
// ****************************************

// ****************************************
// ******** SOURCE, DESTINATION URLS ******
// ****************************************

var srcType;

function GenerateSourceURL($item) {

  var strSrc;
  // Find draggable's url
  var source = $item.find('a').attr('href');
  //Find file name
  var filename = $item.find("img").attr("title");

  var startIndex = source.toString().indexOf('=');
  // Determine if target is folder or file
  if(startIndex == -1){
    // Source is a file
    srcType = "file";
    strSrc = source.toString().replace(L_Menu_BaseUrl+"/", "");
  }
  else{
    srcType = "folder";
    // Source is a folder
    var endIndex = source.toString().indexOf('&');
    strSrc = source.toString().substring(startIndex+1,endIndex);
    // Replace underscore and forward slash characters
    strSrc = strSrc.replace(/%5f/gi, "_");
    strSrc = strSrc.replace(/%2f/gi, "/").replace(L_Menu_BaseUrl+"/", "");
    
  }
    return strSrc;

}// end - function

function GenerateDestinationURL($target, filename) {

  var strDest;
  // Find droppable's url
  var dest = $target.attr("href");
  // Parse destination url
  var startIndex = dest.toString().indexOf('=');
  // Determine if target is folder/breadcrumb or quick launch link
  if(startIndex == -1){
    // Don't append filename if source type is not file
    dest = dest.replace(L_Menu_BaseUrl+"/", "");
    dest = dest.replace("/Forms/AllItems.aspx", "");
    strDest = dest;
  }
  else {
    //alert('folder icon');
    var endIndex = dest.toString().indexOf('&');
    strDest = dest.toString().substring(startIndex+1,endIndex);
    // Replace underscore and forward slash characters
    strDest = strDest.replace(/%5f/gi, "_");
    strDest = strDest.replace(/%2f/gi, "/").replace(L_Menu_BaseUrl+"/", "");
   
  } // end - else

  if(srcType == "file")
    strDest = strDest + "/" + filename;
  else{
    //alert(filename);
    filename = filename.replace("Folder: ", "");
    strDest = strDest + "/" + filename;
  }
    
  return strDest;

}// end - function

// ****************************************
// **** END - SOURCE, DESTINATION URLS ****
// ****************************************

